/****************************************************************************

Force Engine v0.5

Creado: 28/03/08
Clase: Vertex Buffer.inl
Hecho by: German Battiston AKA Melkor

****************************************************************************/

//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline VertexBuffer<PixelFormatClass,FVF>::VertexBuffer()
:
m_pkDevice(NULL),
m_VertexBuffer(NULL)
{

}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline VertexBuffer<PixelFormatClass,FVF>::~VertexBuffer()
{

}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline bool VertexBuffer<PixelFormatClass,FVF>::Create( IDirect3DDevice9 * pkDevice, 
													    bool bDynamic,
													    short sVertexCount, 
													    PixelFormatClass * pkVertexColection )
{
	m_pkDevice = pkDevice;

	m_sVertexCount = sVertexCount;

	HRESULT hr = pkDevice->CreateVertexBuffer( sVertexCount * sizeof(PixelFormatClass),
											   D3DUSAGE_WRITEONLY,
											   FVF,
											   D3DPOOL_DEFAULT,
											   &m_VertexBuffer,
											   NULL );

	void * pVertices = NULL;

	hr = m_VertexBuffer->Lock(0,m_sVertexCount * sizeof(PixelFormatClass),(void **)&pVertices,0);

	memcpy(pVertices, pkVertexColection, sizeof(PixelFormatClass) * m_sVertexCount);

	hr = m_VertexBuffer->Unlock();

	if(hr != D3D_OK)
	{
		return false;
	}

	return true;
}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline void VertexBuffer<PixelFormatClass,FVF>::Release()
{
	
}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline void VertexBuffer<PixelFormatClass,FVF>::Draw( 
													  D3DPRIMITIVETYPE ePrim,
													  int iIndexCount,
													  int iVertexCount )
{
	int iPrimitiveCount = 0;

	if(ePrim == D3DPT_POINTLIST)
	{
		iPrimitiveCount = iIndexCount;
	}
	else if(ePrim == D3DPT_LINELIST)
	{
		iPrimitiveCount = iIndexCount / 2;
	}
	else if(ePrim == D3DPT_LINESTRIP)
	{
		iPrimitiveCount = iIndexCount - 1;
	}
	else if(ePrim == D3DPT_TRIANGLELIST)
	{
		iPrimitiveCount = iIndexCount / 3;
	}
	else if(ePrim == D3DPT_TRIANGLESTRIP)
	{
		iPrimitiveCount = iIndexCount - 2;
	}
	else if(ePrim == D3DPT_TRIANGLEFAN)
	{
		iPrimitiveCount = iIndexCount - 2;
	}

	HRESULT hr = m_pkDevice->DrawIndexedPrimitive(ePrim,0,0,iVertexCount,0,iPrimitiveCount);

	assert(hr == D3D_OK);
}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline void VertexBuffer<PixelFormatClass,FVF>::Bind()
{
	HRESULT hr;
	
	hr = m_pkDevice->SetVertexShader(NULL);
	hr = m_pkDevice->SetFVF(FVF);
	hr = m_pkDevice->SetStreamSource(0, m_VertexBuffer, NULL, sizeof(PixelFormatClass));
}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline LPDIRECT3DVERTEXBUFFER9 VertexBuffer<PixelFormatClass,FVF>::GetVertexBuffer()
{
	return m_VertexBuffer;
}
//---------------------------------------------------------------------------
template <class PixelFormatClass, unsigned int FVF>
inline short VertexBuffer<PixelFormatClass,FVF>::GetVertexCount()
{
	return m_sVertexCount;
}
//---------------------------------------------------------------------------